package org.dromara.answer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.answer.domain.AiOptions;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.answer.domain.bo.AiExamQuestionsBo;
import org.dromara.answer.domain.vo.AiExamQuestionsVo;
import org.dromara.answer.domain.AiExamQuestions;
import org.dromara.answer.mapper.AiExamQuestionsMapper;
import org.dromara.answer.service.IAiExamQuestionsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 考试题目关联Service业务层处理
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@RequiredArgsConstructor
@Service
public class AiExamQuestionsServiceImpl extends ServiceImpl<AiExamQuestionsMapper, AiExamQuestions> implements IAiExamQuestionsService {

    private final AiExamQuestionsMapper baseMapper;

    /**
     * 查询考试题目关联
     *
     * @param id 主键
     * @return 考试题目关联
     */
    @Override
    public AiExamQuestionsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询考试题目关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 考试题目关联分页列表
     */
    @Override
    public TableDataInfo<AiExamQuestionsVo> queryPageList(AiExamQuestionsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AiExamQuestions> lqw = buildQueryWrapper(bo);
        Page<AiExamQuestionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的考试题目关联列表
     *
     * @param bo 查询条件
     * @return 考试题目关联列表
     */
    @Override
    public List<AiExamQuestionsVo> queryList(AiExamQuestionsBo bo) {
        LambdaQueryWrapper<AiExamQuestions> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AiExamQuestions> buildQueryWrapper(AiExamQuestionsBo bo) {
        LambdaQueryWrapper<AiExamQuestions> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExamId() != null, AiExamQuestions::getExamId, bo.getExamId());
        lqw.eq(bo.getQuestionId() != null, AiExamQuestions::getQuestionId, bo.getQuestionId());
        lqw.eq(bo.getQuestionOrder() != null, AiExamQuestions::getQuestionOrder, bo.getQuestionOrder());
        lqw.eq(bo.getQuestionScore() != null, AiExamQuestions::getQuestionScore, bo.getQuestionScore());
        return lqw;
    }

    /**
     * 新增考试题目关联
     *
     * @param bo 考试题目关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AiExamQuestionsBo bo) {
        AiExamQuestions add = MapstructUtils.convert(bo, AiExamQuestions.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改考试题目关联
     *
     * @param bo 考试题目关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AiExamQuestionsBo bo) {
        AiExamQuestions update = MapstructUtils.convert(bo, AiExamQuestions.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AiExamQuestions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除考试题目关联信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void saveOrUpdateByBo(List<AiExamQuestionsBo> examQuestions, Long examId) {

        baseMapper.delete(Wrappers.<AiExamQuestions>lambdaQuery().eq(AiExamQuestions::getExamId, examId));

        List<AiExamQuestions> list = examQuestions.stream().map(map -> {
            AiExamQuestions convert = MapstructUtils.convert(map, AiExamQuestions.class);
            convert.setExamId(examId);
            return convert;
        }).toList();

        baseMapper.insertBatch(list);
    }
}
